<?php

class Modules_Articles extends Module {

	public $name = 'articles';
	public $title = "Статьи";

	public function actions() {
		return array(
			'article' => "Показать статью",
		);
	}

	public function mainActions() {
		return array(
			'article' => "Показать статью",
		);
	}

	protected function _actionproperties(Blocks  $block) {
		switch ($block->action) {
			case 'article':
				return $this->articlesList();
				break;

			default :
				return "Нет";
				break;
		}
	}

	public static function subMenu() {
		return array(
			array(
				'url' => '/admin.php?module=articles',
				'title' => 'Список статей',
				'icon' => '/tpl/admin/images/list.png',
			),
			array(
				'url' => '/admin.php?module=articles&action=create',
				'title' => 'Добавить статью',
				'icon' => '/tpl/admin/images/add_item.png',
			),
		);
	}

	public function articlesList() {
		$block_id = !empty($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
		$articles = ArticlesTable::getInstance()->findAll();

		if ($block_id) {
			$block = BlocksTable::getInstance()->find($block_id);
		} else {
			$block = new Blocks();
			$block->page_id = (int) $_REQUEST['page_id'];
		}

		$this->tpl()->assign(array(
			'articles' => $articles,
			'block' => $block,
		));
		return $this->tpl()->fetch('actionproperties.html');
	}

	public function adminActionIndex() {
		$this->access("r");

		$model = ArticlesTable::getInstance()->findAll();
		$this->tpl()->assign(array(
			'model' => $model
		));
		return $this->tpl()->fetch('index.html');
	}

	public function adminActionCreate() {
		$this->access("c");

		$model = new Articles();

		return $this->_article_edit($model, 'create.html');
	}

	private function _article_edit($model, $tpl) {
		App::registerCoreScript('jquery');
		App::registerCoreScript('tiny_mce');
		App::registerCoreScript('base');

		$this->tpl()->assign(array(
			'model' => $model
		));
		return $this->tpl()->fetch($tpl);
	}

	public function adminActionDelete() {
		$this->access("d");

		$id = (int) $_GET['id'];
		$model = Articles::model()->findByPk($id);
		$model->delete();
		Request::redirect('?module=articles');
	}

	public function adminActionEdit() {
		$this->access("c");

		$id = (int) $_GET['id'];
		if (!$id) Request::redirect(404);

		$model = ArticlesTable::getInstance()->find($id);
		if (!$model) Request::redirect(404);

		if (!empty($model->text)) {
			$model->text = $model->preview . '<!-- pagebreak -->' . $model->text;
		} else {
			$model->text = $model->preview;
		}

		return $this->_article_edit($model, 'update.html');
	}

	public function adminActionSave() {
		$this->access("c");

		$data = $_POST['article'];
		$data['id'] = (int) $data['id'];

		$model = !$data['id'] ? new Articles() : ArticlesTable::getInstance()->find($data['id']);
		$text = explode('<!-- pagebreak -->', $data['text']);
		$model->title = $data['title'];
		$model->preview = $text[0];
		$model->text = isset($text[1]) ? $text[1] : "";

		$model->save();

		if (isset($_REQUEST['apply'])) {
			Request::redirect('?module=articles&action=edit&id=' . $model->id);
		} else {
			Request::redirect('?module=articles&action=index');
		}
	}

	public function actionArticle() {
		$article = (int) $this->block->property('article')->value;
		if (!$article) return "";

		$article = ArticlesTable::getInstance()->find($article);
		if (!$article) return "";

		$tpl = $this->getTpl('article.html');

		$this->tpl()->assign('article', $article);
		return $this->tpl()->fetch($tpl);
	}

}